package com.star.percalservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.star.percalservice.mapper.*;
import com.star.percalservice.model.entity.Award;
import com.star.percalservice.model.entity.Paper;
import com.star.percalservice.model.entity.Patent;
import com.star.percalservice.model.entity.ResearchProject;
import com.star.percalservice.service.ProcessService;
import com.star.percalservice.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author star
 * @version 1.0
 * @Description:
 * @DateTime 2025/4/13 2:15
 */

@Service
public class ProcessServiceImpl implements ProcessService {


    @Autowired
    private ResearchProjectMapper researchProjectMapper;

    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private PatentMapper patentMapper;

    @Autowired
    private AwardMapper awardMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public R list() {
        String status = "pending";
        // 获取科研项目列表
        QueryWrapper<ResearchProject> researchProjectQueryWrapper = new QueryWrapper<>();
        researchProjectQueryWrapper.eq("status", status);
        List<ResearchProject> researchProjectList = researchProjectMapper.selectList(researchProjectQueryWrapper);
        // 论文列表
        QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
        paperQueryWrapper.eq("status", status);
        List<Paper> paperList = paperMapper.selectList(paperQueryWrapper);
        // 专利列表
        QueryWrapper<Patent> patentQueryWrapper = new QueryWrapper<>();
        patentQueryWrapper.eq("status", status);
        List<Patent> patentList = patentMapper.selectList(patentQueryWrapper);
        // 获奖列表
        QueryWrapper<Award> awardQueryWrapper = new QueryWrapper<>();
        awardQueryWrapper.eq("status", status);
        List<Award> awardList = awardMapper.selectList(awardQueryWrapper);
        // 整合所有列表返回
        Map<String, List<?>> map = new HashMap<>();
        map.put("researchProjectList", researchProjectList);
        map.put("paperList", paperList);
        map.put("patentList", patentList);
        map.put("awardList", awardList);
        Map<String, Object> data = new HashMap<>();
        data.put("pending-list", map);
        return R.ok("获取待审核列表成功", data);
    }

    @Override
    public R change(String type, Integer id, String status) {
        if ("pending".equals(status)) {
            return R.error("状态不能为待审核");
        }
        switch (type) {
            case "project" -> {
                QueryWrapper<ResearchProject> researchProjectQueryWrapper = new QueryWrapper<>();
                researchProjectQueryWrapper.eq("id", id);
                ResearchProject researchProject = researchProjectMapper.selectById(id);
                if (researchProject == null) {
                    return R.error("科研项目不存在");
                }
                researchProject.setStatus(status);
                return researchProjectMapper.update(researchProject, researchProjectQueryWrapper) > 0 ? R.ok("修改成功") : R.error("修改失败");
            }
            case "paper" -> {
                QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
                paperQueryWrapper.eq("id", id);
                Paper paper = paperMapper.selectById(id);
                if (paper == null) {
                    return R.error("论文不存在");
                }
                paper.setStatus(status);
                return paperMapper.update(paper, paperQueryWrapper) > 0 ? R.ok("修改成功") : R.error("修改失败");
            }
            case "patent" -> {
                QueryWrapper<Patent> patentQueryWrapper = new QueryWrapper<>();
                patentQueryWrapper.eq("id", id);
                Patent patent = patentMapper.selectById(id);
                if (patent == null) {
                    return R.error("专利不存在");
                }
                patent.setStatus(status);
                return patentMapper.update(patent, patentQueryWrapper) > 0 ? R.ok("修改成功") : R.error("修改失败");
            }
            case "award" -> {
                QueryWrapper<Award> awardQueryWrapper = new QueryWrapper<>();
                awardQueryWrapper.eq("id", id);
                Award award = awardMapper.selectById(id);
                if (award == null) {
                    return R.error("获奖不存在");
                }
                award.setStatus(status);
                return awardMapper.update(award, awardQueryWrapper) > 0 ? R.ok("修改成功") : R.error("修改失败");
            }
            default -> {
                return R.error("类型错误");
            }
        }
    }
}
